"use strict";
import * as chai from "chai";
import * as q from 'q';
import * as co from 'co';
import * as serialport from 'serialport';
import * as fs from 'fs';

import * as TPStest from './../../Model/TPS/index';

var expect = chai.expect;
var portName = "COM4";

// before((done) => {
//     portName.length === 0 && serialport.list(function(err, list) {
//         var reg = /COM\d/;
//         for (let item of list) {
//             if (reg.test(item.comName)) {
//                 portName = item.comName;
//                 break;
//             } else {
//                 console.log('nomatch');
//             }
//         }
//         done();
//     })
// });


describe('全站仪', function() {
    this.timeout(100000);
    console.log('测试前请先正确连接全站仪');
    describe('#获取测站坐标', function() {
        it('全站仪应该开始获取测站坐标', function() {
            return co(function* () {
                yield q.delay(1000);
                var geocom = new TPStest.GeoCom(portName);
                yield q.delay(500);
                var result = yield geocom.getStationCoor();
                geocom.close();
                return result;
            }).then(function(data) {
                expect(data).not.to.equal(null, '应该不返回null,获取测站坐标失败,未能完成geocom指令');
                expect(data.N).not.to.be.equal('number', '测量结果不正确,应该为数字，未能完成geocom指令');
                console.log('StationCor' + JSON.stringify(data));
            })
        })
    })

    describe('#打开激光', function() {
        it('应该返回true', function() {
            return co(function* () {
                yield q.delay(1000);
                var geocom = new TPStest.GeoCom(portName);
                yield q.delay(500);
                var result = yield geocom.toggleLaser('1');
                geocom.close();
                return result;
            }).then(function(data) {
                expect(data).not.to.equal(null, '应该不返回null,打开激光操作失败,未能完成geocom指令');
                expect(data).to.be.equal(true);
            });
        });
    });

    describe('#关闭ATR', function() {
        it('应该返回true', function() {
            return co(function* () {
                yield q.delay(1000);
                var geocom = new TPStest.GeoCom(portName);
                yield q.delay(500);
                var result = yield geocom.toggleATR('0');
                geocom.close();
                return result;
            }).then(function(data) {
                expect(data).not.to.equal(null, '应该不返回null,关闭ATR操作失败,未能完成geocom指令');
                expect(data).to.be.equal(true);
            });
        });
    });

    describe('#打开RL', function() {
        it('应该设置全站仪为无棱镜测量,并且返回true', function() {
            return co(function* () {
                yield q.delay(1000);
                var geocom = new TPStest.GeoCom(portName);
                yield q.delay(500);
                var result = yield geocom.toggleMeaureMethod("RL");
                geocom.close();
                return result;
            }).then(function(data) {
                expect(data).not.to.equal(null, '应该不返回null,设置全站仪为无棱镜失败,未能完成geocom指令');
                expect(data).to.be.equal(true);
            });
        });
    });


    describe('#测量坐标', function() {
        it('全站应该开始测量,并且返回坐标', function() {
            return co(function* () {
                yield q.delay(1000);
                var geocom = new TPStest.GeoCom(portName);
                yield q.delay(500);
                var result = yield geocom.measure();
                geocom.close();
                return result;
            }).then(function(data) {
                expect(data).not.to.equal(null, '应该不返回null,测量失败,未能完成geocom指令');
                expect(data.slope).to.be.a('number', '测量结果并不正确,应该为数字!!');
                console.log('Tps MeasureResult:' + JSON.stringify(data));
            });
        });
    });
    describe('全圆测量测试', function() {
        describe('#学习测量', function() {
            var Tps = new TPStest.TPS();
            it('学习测量数据', function() {
                return co(function* () {
                    yield q.delay(500);
                    var geocom = new TPStest.GeoCom(portName);
                    yield q.delay(500);
                    for (var i = 1; i < 5; i++) {
                        var hz = (i - 1) * Math.PI / 2.5;
                        var hv = i * Math.PI / 6;
                        yield geocom.makePosition(hz, hv, false);
                        var result: TPStest.MeasureResult = yield geocom.measure();
                        console.log(JSON.stringify(result));
                        var pt: TPStest.Coor = Tps.convertToXYZ(result.hz, result.hv, result.slope);
                        var studypt = new TPStest.StudyPoint();
                        if(i === 1) {
                            studypt.isOrient = true ;
                        }
                        studypt.name = i.toString();
                        studypt.e = pt.e;
                        studypt.n = pt.n;
                        studypt.h = pt.h;
                        Tps.studyPointList.push(studypt);
                    }
                    geocom.close();
                    yield q.delay(500);
                    return Tps;
                }).then(function(data) {
                    console.log(data);
                    trySave(JSON.stringify(data));
                })
            });    
            it('全圆测量', function() {
                return co(function* () {
                    var TpsControl = new TPStest.TPSController(portName);
                    yield q.delay(500);
                    TpsControl.tps = Tps;
                    console.log(TpsControl.tps);
                    var surTurn = new TPStest.SurveyTurn();
                    surTurn = yield TpsControl.survey(); 
                    return surTurn;
                }).then(function(data){
                    console.log('测量结果：' + JSON.stringify(data));
                    trySave(JSON.stringify(data));
                })
            })
        });
    });
});

/** 
 * 保存测量结果到文本文件
 * @param pt-每个倾角计测量点
 */
function trySave(value): void {
    try {
		fs.appendFileSync('tpsResult.txt', `\r\n${value}`);
    }
    catch (err) {
        console.log(err);
    }
}

// describe('基础类功能测试', function() {
//     describe('#Vector测试', function() {
//         it('应该能够返回水平角', function() {
//             var pt1 = new TPStest.SurveyPoint(0, 0, 0);
//             var pt2 = new TPStest.SurveyPoint(0, -1, 0);
//             var vector = new TPStest.SurveyVector(pt1, pt2);
//             console.log(`\t水平角是${vector.horizontalAngle}`);
//             expect(vector.horizontalAngle).to.be.a('number', '未能获取水平角');
//         });
//         it('应该能够返回垂直角', function() {
//             var pt1 = new TPStest.SurveyPoint(0, 0, 0);
//             var pt2 = new TPStest.SurveyPoint(0, 0, 1);
//             var vector = new TPStest.SurveyVector(pt1, pt2);
//             console.log(`\t垂直角是${vector.verticalAngle}`);
//             expect(vector.verticalAngle).to.be.a('number', '未能获取垂直角');
//         });
//         it('应该能够返回水平距离', function() {
//             var pt1 = new TPStest.SurveyPoint(0, 0, 0);
//             var pt2 = new TPStest.SurveyPoint(0, -1, 1);
//             var vector = new TPStest.SurveyVector(pt1, pt2);
//             console.log(`\t水平距离是${vector.distance}`);
//             expect(vector.distance).to.be.a('number', '未能获取水平距离');
//         });
//         it('应该能够返回斜距', function() {
//             var pt1 = new TPStest.SurveyPoint(0, 0, 0);
//             var pt2 = new TPStest.SurveyPoint(0, -1, 1);
//             var vector = new TPStest.SurveyVector(pt1, pt2);
//             console.log(`\t斜距是${vector.slope}`);
//             expect(vector.slope).to.be.a('number', '未能获取斜距');
//         });
//     });

//     describe('#TPS测试', function() {
//         it('获取方位角测试', function() {
//             var tps = new TPStest.TPS(100, 2000, 0);
//             expect(tps.center.E).to.be.a('number', '不能能构建一个TPS全站仪类');
//             tps.addPoint('pt1', new TPStest.SurveyPoint(100, 3000, 1));
//             var pt1 = tps.getPoint('pt1');
//             expect(pt1.name).to.be.equal('pt1');
//             var hzhv = tps.convertToAzimuth('pt1');
//             console.log(`\thv:${hzhv.hv},hz:${hzhv.hz}`);
//             expect(hzhv.hz).to.be.a('number');
//             expect(hzhv.hv).to.be.a('number');
//         });
//         it('展点测试,能把hv,hz,slope展成ENH', function() {
//             var tps = new TPStest.TPS(100, 2000, 0);
//             var coor = tps.convertToXYZ(Math.PI / 4, Math.PI * 60 / 180, 100);
//             console.log(`\tE:${coor.E},N:${coor.N},H:${coor.H}`);
//             expect(coor.E).to.be.a('number');
//             expect(coor.N).to.be.a('number');
//             expect(coor.H).to.be.a('number');
//         });
//     });

//     describe('#深度拷贝测试', function() {
//         it('应当能够正确的深度拷贝对象,修改一个对象不会影响另外一个', function() {
//             var a = new TPStest.SurveyPoint(1, 2, 3);
//             var b = Object.assign(new TPStest.SurveyPoint(), a);
//             console.log(`\t拷贝后a.E=${a.E},b.E=${b.E}`);
//             expect(a.E).to.be.eq(b.E);
//             a.E = 100;
//             console.log(`\t修改后a.E=${a.E},b.E=${b.E}`);
//             expect(a.E).to.be.not.eq(b.E);
//         });
//     });
// })
